package com.ruoyi.product.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.domain.Result;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.es.domain.SkuInfoEsModel;
import com.ruoyi.es.remote.ProductEsRemote;
import com.ruoyi.product.domain.vo.PmsSkuInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.product.mapper.PmsSpuInfoMapper;
import com.ruoyi.product.domain.PmsSpuInfo;
import com.ruoyi.product.service.IPmsSpuInfoService;

/**
 * spu信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-14
 */
@Service
public class PmsSpuInfoServiceImpl implements IPmsSpuInfoService
{
    @Autowired
    private PmsSpuInfoMapper pmsSpuInfoMapper;

    @Autowired
    private ProductEsRemote productEsRemote;

    @Override
    public void up(Long spuId) {
        List<PmsSkuInfoVO> list = pmsSpuInfoMapper.getSkuInfo(spuId);

        List<SkuInfoEsModel> skuInfoEsModels = new ArrayList<>();
        for (PmsSkuInfoVO vo : list) {
            SkuInfoEsModel model = new SkuInfoEsModel();
            BeanUtils.copyProperties(vo,model);


            List<PmsSkuInfoVO.AttrVO> attrs = vo.getAttrs();
            List<SkuInfoEsModel.Attr> collect = attrs.stream().map(attrVO -> {
                SkuInfoEsModel.Attr attr = new SkuInfoEsModel.Attr();
                BeanUtils.copyProperties(attrVO, attr);
                return attr;
            }).collect(Collectors.toList());
            model.setAttrs(collect);
            model.setHasScore(100L);
            model.setHasStock(true);
            skuInfoEsModels.add(model);
        }
        Result<Boolean> result = productEsRemote.addBatch(JSONArray.toJSONString(skuInfoEsModels));

        if (result.getCode()== Constants.ERROR){
            throw new RuntimeException("上架失败");
        }

        /**
         * 修改订单状态
         */
        pmsSpuInfoMapper.updateStatus(spuId);
    }

    /**
     * 查询spu信息
     *
     * @param id spu信息主键
     * @return spu信息
     */
    @Override
    public PmsSpuInfo selectPmsSpuInfoById(Long id)
    {
        return pmsSpuInfoMapper.selectPmsSpuInfoById(id);
    }

    /**
     * 查询spu信息列表
     *
     * @param pmsSpuInfo spu信息
     * @return spu信息
     */
    @Override
    public List<PmsSpuInfo> selectPmsSpuInfoList(PmsSpuInfo pmsSpuInfo)
    {
        return pmsSpuInfoMapper.selectPmsSpuInfoList(pmsSpuInfo);
    }

    /**
     * 新增spu信息
     *
     * @param pmsSpuInfo spu信息
     * @return 结果
     */
    @Override
    public int insertPmsSpuInfo(PmsSpuInfo pmsSpuInfo)
    {
        pmsSpuInfo.setCreateTime(DateUtils.getNowDate());
        return pmsSpuInfoMapper.insertPmsSpuInfo(pmsSpuInfo);
    }

    /**
     * 修改spu信息
     *
     * @param pmsSpuInfo spu信息
     * @return 结果
     */
    @Override
    public int updatePmsSpuInfo(PmsSpuInfo pmsSpuInfo)
    {
        pmsSpuInfo.setUpdateTime(DateUtils.getNowDate());
        return pmsSpuInfoMapper.updatePmsSpuInfo(pmsSpuInfo);
    }

    /**
     * 批量删除spu信息
     *
     * @param ids 需要删除的spu信息主键
     * @return 结果
     */
    @Override
    public int deletePmsSpuInfoByIds(Long[] ids)
    {
        return pmsSpuInfoMapper.deletePmsSpuInfoByIds(ids);
    }

    /**
     * 删除spu信息信息
     *
     * @param id spu信息主键
     * @return 结果
     */
    @Override
    public int deletePmsSpuInfoById(Long id)
    {
        return pmsSpuInfoMapper.deletePmsSpuInfoById(id);
    }
}
